package com.tracker.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;


public class TrackerConnection {
	
	private BasicDataSource datasource;

	public TrackerConnection() {
		this.datasource = new BasicDataSource();
		datasource.setDriverClassName("com.mysql.jdbc.Driver");
		datasource.setUrl("jdbc:mysql://localhost:3306/tracker");
		datasource.setUsername("root");
		datasource.setPassword("sa");
	}
	
	public Connection getConnection() throws SQLException {
		/*Connection conn = null;
		String url = "jdbc:mysql://localhost:3306/";
		String dbName = "tracker";
		String driver = "com.mysql.jdbc.Driver";
		String userName = "root"; 
		String password = "sa";
		try {
			Class.forName(driver).newInstance();
			conn = DriverManager.getConnection(url+dbName,userName,password);
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		return this.datasource.getConnection();
	}
	
	public void closeConnection(Connection con) {
		try {
			if(con!= null) {
				con.close();
				con=null;
			}
		} catch(Exception e) {
			
		}
	}
	
	public void closePreparedStatement(PreparedStatement stmt) {
		try {
			if(stmt!= null) {
				stmt.close();
				stmt=null;
			}
		} catch(Exception e) {
			
		}
	}
	
	public void closeStatement(Statement stmt) {
		try {
			if(stmt!= null) {
				stmt.close();
				stmt=null;
			}
		} catch(Exception e) {
			
		}
	}
	
	public void closeResultset(ResultSet rs) {
		try {
			if(rs!= null) {
				rs.close();
				rs=null;
			}
		} catch(Exception e) {
			
		}
	}
	
}
